from database import TwitterUser,FetchHelper
from datetime import datetime
from google.appengine.ext import db
from twitter import Twitter
import re,types
import simplejson as json

def init(name):
    """ fetch initialization, save a new user in FetchHelper """
    t = Twitter()
    result = t.getFriends(name, -1)
    if type(result) == types.DictType:
        save(result)
        initHelper(name, result["next_cursor"])
        return True
    else:
        return False

def initHelper(name, cursor):
    """ init database FetchHelper, only called by init()"""
    q = db.GqlQuery("SELECT * FROM FetchHelper")
    results = q.fetch(100)
    for result in results:
        result.delete()
    
    fetchHelper = FetchHelper()
    fetchHelper.screen_name = name
    fetchHelper.cursor = cursor
    fetchHelper.readtype = False
    
    fetchHelper.put()


def fetch():
    """ fetch data """
    fetchHelper = db.GqlQuery("SELECT * FROM FetchHelper LIMIT 0,1")[0]
    name = fetchHelper.screen_name
    cursor = fetchHelper.cursor
    
    t = Twitter()
    result = t.getFriends(name, cursor)
    if type(result) == types.DictType:
        if "users" in result: save(result)
        next(name, result)
    else:
        if result == 408:
            #request timeout
            pass
        else:
            next(name, result)
    
    
def save(result):
    """ save user data to database, param result is dict """
    timeFormat = "%a %b %d %H:%M:%S +0000 %Y"   
    chinese = re.compile(ur"[\u4e00-\u9fa5]").search
    japanese = re.compile(ur"[\u3040-\u309F\u30A0-\u30FF]").search
    
    for user in result['users']:
        
        if "status" in user:
            testStr = user["status"]["text"]
        else:
            testStr = user["description"] and user["description"] or ""
        
        if chinese(testStr) and not japanese(testStr):
            exist = db.GqlQuery("SELECT * FROM TwitterUser WHERE id = :1 LIMIT 0,1", user["id"])
            if exist.count() > 0:
                twitterUser = getOne(exist)
            else:
                twitterUser = TwitterUser()
        
            twitterUser.id = user["id"]
            twitterUser.name = user["name"]
            twitterUser.screen_name = user["screen_name"]
            twitterUser.location = user["location"]
            twitterUser.description = user["description"]
            twitterUser.profile_image_url = user["profile_image_url"]
            twitterUser.url = user["url"]
            twitterUser.user_created_at = datetime.strptime(user["created_at"], timeFormat)
            
            twitterUser.followers_count = user["followers_count"]
            twitterUser.friends_count = user["friends_count"]
            twitterUser.statuses_count = user["statuses_count"]
            
            if user["protected"]:
                twitterUser.is_protected = True
            else:
                if "status" in user:
                    twitterUser.text = user["status"]["text"]
                    twitterUser.text_created_at = datetime.strptime(user["status"]["created_at"], timeFormat)
                    twitterUser.text_id = user["status"]["id"]
                    twitterUser.text_source = user["status"]["source"]
                twitterUser.is_protected = False
                #twitterUser.readed = False
            twitterUser.put()
        
    
        
def next(name, result):
    """ store the next cursor to FetchHelper, if no next_cursor, call changeUser"""
    #result is list or int(http error code)
    if type(result) == types.DictType and "next_cursor" in result and result["next_cursor"] > 0:
        fetchHelper = getOne(db.GqlQuery("SELECT * FROM FetchHelper LIMIT 0,1"))
        fetchHelper.cursor = result["next_cursor"]
        fetchHelper.put()
    else:
        changeUser()
        
def changeUser():
    """ store a new user to FetchHelper """
    fetchHelper = getOne(db.GqlQuery("SELECT * FROM FetchHelper LIMIT 0,1"))
    twitterUser = db.GqlQuery("SELECT * FROM TwitterUser WHERE screen_name = :1 LIMIT 0,1", fetchHelper.screen_name)
    for t in twitterUser:
        t.readed = True
        t.put()
        
    newTwitterUser = getOne(db.GqlQuery("SELECT * FROM TwitterUser WHERE readed = " + str(fetchHelper.readtype) + " AND friends_count > 50 ORDER BY friends_count DESC LIMIT 10,1"))
    if newTwitterUser:
        fetchHelper.screen_name = newTwitterUser.screen_name
        fetchHelper.cursor = -1
        fetchHelper.put()
    else:
        fetchHelper.readtype = (not fetchHelper.readtype)
        fetchHelper.put()


def getOne(result):
    """ in GqlQuery return, result[0] is not the right value, can not save, must use for in """
    for i in result:
        return i
    return False


if __name__ == '__main__':
  fetch()